package com.time.workshop.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.time.workshop.API;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.bean.RequestOrder;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.LaoBanDialog;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.OnClickOKListener;
//git.oschina.net/fanbao/WorkShop.git
/**
 * 老人专区
 * @author wangfeng
 *
 */
public class LaoRenActivity extends SuperActivity implements OnClickListener{
	@V
	private ImageView img_login_back,laoren_order_infoImageView;
	@V
	private TextView laoren_title,laoren_order_commitOrderTextView;
	@V
	private RelativeLayout laoren_order_time_Layout,laoren_order_name_Layout,laoren_order_phone_Layout,
	laoren_order_age_Layout,laoren_order_address_Layout, laoren_order_remarks_Layout;
	@V
	private TextView laoren_order_timeTextView ,titlename;
	@V
	private EditText laoren_order_nameEditText,laoren_order_ageEditText,laoren_order_phoneEditText,
	             laoren_order_addressEditText,laoren_order_remarksEditText;

	private DatePicker datePicker;  
	private TimePicker timePicker;  
	private AlertDialog dialog;  
	public Calendar time = Calendar.getInstance(Locale.CHINA);  
	private boolean processFlag = true;

	private String mType = "";

	private String mtime,name,age,phone,address,remarke;
	
	private String type = "";	
	
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_laoren);

		mType = getIntent().getStringExtra("type");

		init();

		initView();
	}
	private void init() {
		Injector.getInstance().inject(this);
		
		laoren_title.setText(mType);
		titlename.setText(mType);

		img_login_back.setOnClickListener(this);
		laoren_order_time_Layout.setOnClickListener(this);
		laoren_order_commitOrderTextView.setOnClickListener(this);

		laoren_order_time_Layout.getBackground().setAlpha(229);//90%透明度
		laoren_order_name_Layout.getBackground().setAlpha(204);//80%透明度
		laoren_order_phone_Layout.getBackground().setAlpha(153);//60%透明度
		laoren_order_age_Layout.getBackground().setAlpha(127);//50%透明度
		laoren_order_address_Layout.getBackground().setAlpha(127);//50%透明度
		laoren_order_remarks_Layout.getBackground().setAlpha(102);//40%透明度
		laoren_order_commitOrderTextView.getBackground().setAlpha(153);//60%透明度

	}

	private void initView(){
		
		if(getString(R.string.chatting).equals(mType)){

			type = "LRPL";
			laoren_order_infoImageView.setImageResource(R.drawable.liaotian);
			laoren_order_age_Layout.setVisibility(View.VISIBLE);
			laoren_order_remarks_Layout.setVisibility(View.GONE);
			laoren_order_timeTextView.setHint(getString(R.string.laoren_hint_laoke_time));
		
			
		}else if(getString(R.string.shopping).equals(mType)){	
			
			type = "LRMC";
			laoren_order_infoImageView.setImageResource(R.drawable.maicai);
			laoren_order_age_Layout.setVisibility(View.GONE);
			laoren_order_remarks_Layout.setVisibility(View.VISIBLE);
			laoren_order_timeTextView.setHint(getString(R.string.laoren_hint_shop_time));
			laoren_order_remarksEditText.setHint(getString(R.string.laoren_remark_hint_shop));
			
			
		}else if(getString(R.string.cleaning).equals(mType)){
			
			type = "LRDS";
			laoren_order_infoImageView.setImageResource(R.drawable.dasao);
			laoren_order_age_Layout.setVisibility(View.GONE);
			laoren_order_remarks_Layout.setVisibility(View.VISIBLE);
			laoren_order_timeTextView.setHint(getString(R.string.laoren_hint_dasao_time));
			laoren_order_remarksEditText.setHint(getString(R.string.laoren_remark_hint_dasao));
			
			
		}


	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_login_back:
			CommonUtils.hide(this);
			back();
			break;

		case R.id.laoren_order_time_Layout: //选择时间

			if (processFlag == true) {
				dateTimePickerDialog();  
				processFlag = false;
				new TimeThread().start(); 
			}

			break;

		case R.id.laoren_order_commitOrderTextView:// 提交订单

			Judge();
			
			break;
		default:
			break;
		}
	}

	private void Judge() {

		mtime = laoren_order_timeTextView.getText().toString().trim();
		name = laoren_order_nameEditText.getText().toString().trim();
		age = laoren_order_ageEditText.getText().toString().trim();
		phone = laoren_order_phoneEditText.getText().toString().trim();
		address = laoren_order_addressEditText.getText().toString().trim();
		remarke = laoren_order_remarksEditText.getText().toString().trim();
		
		if("".equals(CommonUtils.isNull(name))){
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_run_time));
			return;
		}
		
		if("".equals(CommonUtils.isNull(name))){
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_name));
			return;
		}
		
		if (!CommonUtils.isMobile(phone)) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_phone));
			return;
		}
		if ("".equals(CommonUtils.isNull(address))) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_address));
			return;
		}
		if(!mType.equals(getString(R.string.chatting)) ){
			
		}else {

			if ("".equals(CommonUtils.isNull(age))) {
				NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_age));
				return;
			}
			int  mAge = Integer.parseInt(age);
			if(mAge > 150 || mAge < 0){
				NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_correct_age));
				return;
			}
		}
		
		showConfirmDialog();
		
	}

	private LaoBanDialog dlg;
	/**
	 * 显示  Dailog 
	 */
	private void showConfirmDialog() {

		dlg = new LaoBanDialog(LaoRenActivity.this);
		if (dlg.isShowing()) {
			dlg.dismiss();
			return;
		}
		dlg.onCreate();
		dlg.setOnClickOK(new OnClickOKListener() {

			@Override
			public void setOnClickOK() {
				dlg.dismiss();
				RequestOrder requestOrder = new RequestOrder();
				requestOrder.ORDER_TYPE = type;
				requestOrder.contactor_address = address;
				requestOrder.contactor_person = name;
				requestOrder.contactor_phone = phone;
				requestOrder.remark = remarke;
				requestOrder.AGE = age;
				requestOrder.reservation_time = mtime;
				API.submitOrder(requestOrder, LaoRenActivity.this);
			}
		});
	}

	//选择时间
	public AlertDialog dateTimePickerDialog(){  
		LinearLayout dateTimeLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.date_time_picker, null);  
		datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.DatePicker);  
		timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.TimePicker);  
		timePicker.setIs24HourView(true);  

		OnTimeChangedListener timeListener= new OnTimeChangedListener() {  
			@Override  
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {  
				time.set(Calendar.HOUR_OF_DAY, hourOfDay);  
				time.set(Calendar.MINUTE, minute);  

			}  
		};  

		timePicker.setOnTimeChangedListener(timeListener);  
		OnDateChangedListener dateListener = new OnDateChangedListener() {  

			@Override  
			public void onDateChanged(DatePicker view, int year, int monthOfYear,  
					int dayOfMonth) {  
				time.set(Calendar.YEAR, year);  
				time.set(Calendar.MONTH, monthOfYear);  
				time.set(Calendar.DAY_OF_MONTH, dayOfMonth);  
			}  
		};  

		datePicker.init(time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH), dateListener);  
		timePicker.setCurrentHour(time.get(Calendar.HOUR_OF_DAY));  
		timePicker.setCurrentMinute(time.get(Calendar.MINUTE));  

		dialog = new AlertDialog.Builder(this).setTitle("设置日期时间").setView(dateTimeLayout)  
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
					@Override  
					public void onClick(DialogInterface dialog, int which) {  

						time.set(Calendar.YEAR, datePicker.getYear());  
						time.set(Calendar.MONTH, datePicker.getMonth());  
						time.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());  
						time.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());  
						time.set(Calendar.MINUTE, timePicker.getCurrentMinute());  
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
						laoren_order_timeTextView.setText(format.format(time.getTime()));
					}  
				}).setNegativeButton("取消", new DialogInterface.OnClickListener() {  
					@Override  
					public void onClick(DialogInterface dialog, int which) {  

					}  
				}).show();  
		return dialog;  
	} 

	private class TimeThread extends Thread {    
		public void run() {  
			try {  
				Thread.sleep(2000);  
				processFlag = true;  
			} catch (Exception e) {  
				e.printStackTrace();  
			}  
		}  
	} 


}
